package com.xcb.home.bussiness;

import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.plat.pool.LogicFacadeBase;
import com.ums.umsmsg.UmsMsg;
import com.ums.umsmsg.proc.Trans;
import com.xcb.home.dao.UserRegisterMapper;

public class UserBusiness extends LogicFacadeBase{
	private static Logger log = Logger.getLogger(UserBusiness.class);	
	
	
	@Autowired
	UserRegisterMapper userRegisterMapper;
	
	
	

	@Override
	public boolean packInMsg() {
		log.info("组 请求端 返回报文..."+getCFG(Trans.IN));
		try {
			inRspMsg =UmsMsg.pack(getCFG(Trans.IN), inMap);
			return ((inRspMsg.length > 0)? true : false);
		} catch (Exception e) {
			log.error("组 请求端 返回报文异常["+e+"]");
			return false;
		}
	}
	@Override
	public boolean transAuth() {
		return true;
	}

	@Override
	protected void setXYM() {
		ZD_XYM = "respCode";
		ZD_XYXX = "respMsg";
	}

	
	
	
	public boolean verPhonMess(){
		log.info("开始验证短信验证码");
		return true;
	}
	
	
	
	
	
	
	
	
	public boolean verInSig() {
		return true;
	}
	public boolean unpackInMsg() {
		log.info("拆 请求端 请求报文...");
		try {
			Map<String, String> tmpMap = UmsMsg.unpack(getCFG(Trans.IN), inReqMsg);
			inMap.putAll(tmpMap);
			return ((tmpMap.size() > 0)? true : false);
		} catch (Exception e) {
			log.error("拆 请求端 请求报文异常["+e+"]");
			inMap.put("respCode", "S00003");
			inMap.put("respMsg", "请求数据异常");
			return false;
		}
	}
	
	
	
	
	public boolean getMerchAndPoenRegIs(){
		log.info("验证该注册手机号是否已经注册过");
		
		try {
			String phoneDyMerchId = userRegisterMapper.getPhoneDyMerchId(inMap.get("phone").toString());
			if(phoneDyMerchId!=null && !phoneDyMerchId.equals("")){
				log.info("该手机号已经注册完成");
				
				inMap.put("respCode", "000001");
				inMap.put("respMsg", "用户已经注册");
				return false;
			}
			inMap.put("merch_id",phoneDyMerchId );
			
			log.info("该用户无注册记录，开始获取父级商户号");
			String fatherMerch = userRegisterMapper.getPhoneDyMerchId(inMap.get("promoters").toString());
			log.info("获取父级商户号完成["+phoneDyMerchId+"]");
			inMap.put("father_merchId",fatherMerch );
			
			log.info("获取新商户号");
			String newMerch = userRegisterMapper.GetNewMerhc();
			log.info("获取新商户号完成["+phoneDyMerchId+"]");
			
			inMap.put("newMerchId", getMerch(newMerch,"00000000000000"));
			
		} catch (Exception e) {
			log.error("在进行验证手机号是否注册、获取新商户号、获取父级商户异常["+e+"]");
			inMap.put("respCode", "000002");
			inMap.put("respMsg", "用户信息异常，注册失败");
			return false;
		}
		
		
		return true;
	}
	
	public boolean insertMerchInfo(){
		try {
			int insertAndUpdaUserInfo = userRegisterMapper.insertAndUpdaUserInfo(inMap);
			log.info("插入商户信息完成,受影响行数["+insertAndUpdaUserInfo+"]");
			if(insertAndUpdaUserInfo!=1){
				inMap.put("respCode", "000002");
				inMap.put("respMsg", "用户信息异常，注册失败");
				return false;
			}
			
			inMap.put("respCode", "000000");
			inMap.put("respMsg", "注册成功");
			return true;
			
		} catch (Exception e) {
			log.error("插入用户注册数据异常["+e+"]");
			return false;
		}
		
	}
	
	@SuppressWarnings("unchecked")
	public boolean getMerchInfo(){
		Map merInfoMap=null;
		try {
			merInfoMap = userRegisterMapper.getMerchInfo(inMap);
		} catch (Exception e) {
			log.error("查询用户信息异常"+e);
			inMap.put("respCode", "000002");
			inMap.put("respMsg", "用户信息异常，注册失败");
			return false;
		}
		if(merInfoMap!=null || !"".equals(merInfoMap)){
			inMap.putAll(merInfoMap);
		}else{
			log.info("未查询到该用户信息");
			inMap.put("respCode", "000002");
			inMap.put("respMsg", "用户信息异常，注册失败");
			return false;
		}
		return true;
	}
	
	
	public String getMerch(String merch,String Templ){
		try {
			StringBuffer newMerch=new StringBuffer();
			newMerch.append("8");
			for(int i=0;i<Templ.length()-merch.length();i++){
				newMerch.append("0");
			}
			newMerch.append(merch);
			return newMerch.toString();
		} catch (Exception e) {
			System.out.println("格式化商户号异常["+e+"]");
		}
		return merch;
	}
	
}
